import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex); //vue的插件机制
import {
	getUrl
} from '@/config.js'
import axios from 'axios';
//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		"userInfo": {},
		"token": '',
		// tabbar配置
		"list": [{
				"pagePath": "pages/index/index",
				"iconPath": "/static/index/btnButtom_index1.png",
				"selectedIconPath": "/static/index/btnButtom_index2.png",
				// "text": "首页"
			},
			{
				"pagePath": "pages/middleScreening/index",
				"iconPath": "/static/tabBar/shaicha.png",
				"selectedIconPath": "/static/tabBar/shaicha.png",
				"text": "去筛查"
			},
			{
				"pagePath": "pages/personal/index",
				"iconPath": "/static/index/btnButtom_mine1.png",
				"selectedIconPath": "/static/index/btnButtom_mine2.png",
				// "text": "个人中心"
			}
		],
		selIdx: 0,
		promotionCode: '', // N8WY   生产
		webUrl: '',
	},
	mutations: {
		set_user_info(state, data) {
			state.userInfo = data
		},
		set_token(state, data) {
			state.token = data
		},
		set_selIdx(state, data) {
			state.selIdx = data
		},
		set_promotionCode(state, data) {
			state.promotionCode = data
		},
		set_web_url(state, data) {
			state.webUrl = data
		}
	},
	actions: {
		// 算是推出登录了
		clearStorage({
			commit,
		}) {
			localStorage.clear()
			commit('set_user_info', {})
			commit('set_token', '')
		},
		checkUser({
			commit,
			dispatch,
		}) {
			// 判断是否登录过期或者怎么样
			return new Promise((resolve, reject) => {
				const token = localStorage.getItem('token');
				// const token = state.token
				if (token) {
					axios.request({
						url: getUrl() + '/an/wechat_app/wechat_user/info' + `?token=${token}`,
						success: (res) => {
							if (res.data.code !== 200) {
								reject()
								dispatch('clearStorage')
							} else {
								console.log(res, 'resresresresresresres')
								const user = res.data.rows[0]
								console.log(user, 'useruseruseruser')
								if (user.sex == 1) {
									if (user.nickName == '' && user.userIcon == "") {
										const userInfo = {
											nickName: '微信用户' + user.wechatUserId,
											avatarUrl: 'https://www.ainaoxingdong.com/api/an-media/images/an/static/personal/boy.jpg',
											isSuggest: user.isSuggest,
											sex: user.sex
										}
                                        localStorage.setItem("userInfo",JSON.stringify(
											userInfo))
										// uni.setStorageSync("userInfo", JSON.stringify(
										// 	userInfo));
										commit("set_user_info", userInfo);
									} else {
										const userInfo = {
											nickName: user.nickName,
											avatarUrl: user.userIcon,
										}
                                        localStorage.setItem("userInfo",JSON.stringify(
											userInfo))
										commit("set_user_info", userInfo);
									}
								} else {
									console.log('是女')
									if (user.nickName == '' && user.userIcon == "") {
										const userInfo = {
											nickName: '微信用户' + user.wechatUserId,
											avatarUrl: 'https://www.ainaoxingdong.com/api/an-media/images/an/static/personal/girl.jpg',
											isSuggest: user.isSuggest,
											sex: user.sex
										}
                                        localStorage.setItem("userInfo",JSON.stringify(
											userInfo))
										commit("set_user_info", userInfo);
									} else {
										const userInfo = {
											nickName: user.nickName,
											avatarUrl: user.userIcon,
										}
                                        localStorage.setItem("userInfo",JSON.stringify(
											userInfo))
										commit("set_user_info", userInfo);
									}
								}

								resolve()
							}
						}
					})
				} else {
					dispatch('clearStorage')
					reject()
				}
			})

		}
	}
})
export default store
